#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "framebuffer.h"
#include "game.h"

#define ROW 10
#define LINE 10
#define SIZE 10
#define MASK 10
#define ANNSIZE 1
#define FILENAMESIZE 256

#define PATHNAME "/home/ll/Pictures"

int map[ROW][LINE];
int disMap[ROW][LINE];
struct point_st mapPoint[ROW][LINE];
struct point_st anN[ANNSIZE];
int disAnN[ANNSIZE] = {0};
int sg_flag = 0;
int sg_first = 0;
int bannar = 0;

void loadPictures(int x, int y, int pos_x, int pos_y)
{
	char filename[FILENAMESIZE] = {0};
	sprintf(filename,"%s/%d%s",PATHNAME, disMap[x][y], ".bmp");
	lcdDisplayPictures(filename,pos_x, pos_y);
}

void flushMap()
{
	int i, j;
	int pos_x, pos_y;
	char filename[FILENAMESIZE] = {0};

	for (i = 0, pos_y = 0; i < ROW; i++, pos_y += 48)
	{
		for (j = 0, pos_x = 175; j < LINE; j++, pos_x += 48)
		{
			mapPoint[i][j].x = pos_x;
			mapPoint[i][j].y = pos_y;
			loadPictures(i, j, pos_x, pos_y);
		}
		
	}
/*
	for (i = 0; i < ANNSIZE; i++)
	{
		if (disAnN[i] != 0)
		{
			sprintf(filename,"%s/%d%s",PATHNAME, disAnN[i], ".bmp");
			lcdDisplayPictures(filename,anN[i].x, anN[i].y);
		}
		
	}
*/
	sprintf(filename,"%s/%d%s",PATHNAME, disAnN[0], ".bmp");
	lcdDisplayPictures(filename,anN[0].x, anN[0].y);
}

void showMap(int x, int y)
{
	int i, j;

	setBackground(0xffffff);
	lcdDisplayPictures("/home/ll/Pictures/11.bmp", 0, 0);

	sleep(2);
	setBackground(0xffffff);
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < LINE; j++)
		{
			if (map[i][j] < 0 && disMap[i][j] != 16)
				disMap[i][j] = 9;
		}
	}
	flushMap();
}

static void chect()
{
	
}

void spaceSprend(int x, int y)
{

	if (x < 0 || y < 0 || x >= ROW || y >= LINE || disMap[x][y] != 10 || map[x][y] < 0)
	{
		if (disMap[x][y] != 15)
			return ;
		else
			bannar--;
	}

	if (map[x][y] > 0)
	{
		disMap[x][y] = map[x][y];
		return ;
	}
	
	disMap[x][y] = 0;

	spaceSprend(x, y-1);
	spaceSprend(x, y+1);
	spaceSprend(x-1, y);
	spaceSprend(x+1, y);
	spaceSprend(x-1, y-1);
	spaceSprend(x-1, y+1);
	spaceSprend(x+1, y-1);
	spaceSprend(x+1, y+1);
	
	return ;
}

void getZoom(int x, int y)
{
	if (x < 0 || y < 0 || x >= ROW || y >= LINE)
		return ;
	if (map[x][y] != -1)
		map[x][y] += 1;
	else
		return ;
}

struct point_st getIndex(int x, int y)
{
	struct point_st pos;
	int i, j;

	pos.x = pos.y = -1;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < LINE; j++)
		{
			if (x >= mapPoint[i][j].x && x <= mapPoint[i][j].x + 40 \
				&& y >= mapPoint[i][j].y && y <= mapPoint[i][j].y + 40)	
			{
				pos.x = i;
				pos.y = j;
				return pos;
			}
		}
	}
	
	for (i = 0; i < ANNSIZE; i++)
	{
		if (x >= anN[i].x && x <= anN[i].x + 80 \
			&& y >= anN[i].y && y <= anN[i].y + 80)	
		{
			if (i == 0)
			{
				if (sg_flag == 1)
					sg_flag = 0;
				else
					sg_flag = 1;
			}
//			
			if (disAnN[0] == 12)
				disAnN[0] = 13;
			else
				disAnN[0] = 12;
			sg_first = 1;
			pos.x = i;
			pos.y = i;
			return pos;
		}
	
	}
	return pos;
}

int GameInit()
{
	int i, j, k;
	int x, y;
	int count = 0;
	struct point p; 
	struct point_st index;
	
	initEquipment();

	srand((unsigned)time(NULL));
	while (count < SIZE)
	{
		i = rand() % 10;
		j = rand() % 10;
		if (map[i][j] == 0)
		{
			map[i][j] = -1;
			count++;
		}
	}
	
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < LINE; j++)	
		{
			if (map[i][j] == -1)
			{
				getZoom(i, j-1);
				getZoom(i, j+1);
				getZoom(i-1, j);
				getZoom(i+1, j);
				getZoom(i-1, j-1);
				getZoom(i-1, j+1);
				getZoom(i+1, j-1);
				getZoom(i+1, j+1);
			}
		}
	}

	for (i = 0; i < ROW; i++)	
	{
		for (j = 0; j < LINE; j++)
		{
			disMap[i][j] = 10;
		}
	}
/*
	for (i = 0; i < ANNSIZE; i++)
	{
	}
*/
	anN[0].x = 700;
   	anN[0].y = 48;
	disAnN[0] = 12;	
	flushMap();

	while (1)
	{
		sg_first = 0;
		p = clickTouch();
		index = getIndex(p.x, p.y);
		if (index.x == -1 || index.y == -1)
		{
			continue;
		}
		else
		{
			x = index.x;
			y = index.y;
		}
		
		if (sg_flag == 0 && sg_first != 1)
		{
			if (map[x][y] < 0)
			{
				disMap[x][y] = 16;
				showMap(x, y);
				break;
			}
			else if (map[x][y] == 0)
			{
				spaceSprend(x, y);
			}
			else if (map[x][y] > 0)
				disMap[x][y] = map[x][y];
		}
		else
		{
			if (sg_first != 1 && (disMap[x][y] == 10 || disMap[x][y] == 15))
			{
				if (disMap[x][y] == 15)
				{
					disMap[x][y] = 10;
					bannar--;
				}
				else if (bannar < SIZE && disMap[x][y] == 10)
				{
					disMap[x][y] = 15;
					bannar++;
				}
			}
		}
		if (bannar == SIZE)
		{
			gameWin();
		}

		flushMap();
	}
	
	closeEquipment();
}

void gameWin()
{
	int i, j;
	int n = 0;
	
	for (i = 0; i < ROW; i++)	
	{
		for (j = 0; j < LINE; j++)
		{
			if (disMap[i][j] == 15 && map[i][j] < 0)
			{
				n++;
			}
		}
	}
	if (n == SIZE)
	{
		setBackground(0xffffff);
		lcdDisplayPictures("/home/ll/Pictures/14.bmp", 0, 0);
		exit(0);
	}
}
